/*
 * ChatFrame.java
 *
 * Created on January 27, 2004, 10:13 PM
 */

package gov.nist.applet.phone.ua.gui;

import gov.nist.applet.phone.media.MediaManager;
import gov.nist.applet.phone.media.messaging.CaptureScreen;
import gov.nist.applet.phone.media.messaging.MediaPlayer;
import gov.nist.applet.phone.media.messaging.SaveAudioVideo2File;
import gov.nist.applet.phone.security.filetransfer.FileTransferDialog;
import gov.nist.applet.phone.security.filetransfer.ReceiveFile;
import gov.nist.applet.phone.security.filetransfer.SendFile;
import gov.nist.applet.phone.ua.ChatSessionManager;
import gov.nist.applet.phone.ua.Configuration;
import gov.nist.applet.phone.ua.MessengerManager;
import gov.nist.applet.phone.ua.call.AudioCall;
import gov.nist.applet.phone.ua.call.Call;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.media.IncompatibleSourceException;
import javax.media.Manager;
import javax.media.protocol.DataSource;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;



/**
 * 
 * @author DERUELLE Jean
 */
public class ChatFrame extends javax.swing.JFrame {
	String contactAddress = null;
	MessengerManager sipMeetingManager = null;
	MediaManager mediaManager = null;
	ChatSessionManager chatSessionManager = null;
	IncomingMessageFrame incomingMessageFrame = null;

	/** Creates new form ChatFrame */
	public ChatFrame(Object parent, String contactAddress,
			MessengerManager sipMeetingManager,
			ChatSessionManager chatSessionManager) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception exe) {
			exe.printStackTrace();
		}
		this.chatSessionManager = chatSessionManager;
		this.contactAddress = contactAddress.trim().toLowerCase();
		this.sipMeetingManager = sipMeetingManager;
		this.mediaManager.chatFram = this;
		initComponents();
		/*
		 * if (parent instanceof JApplet) { JApplet applet = (JApplet) parent;
		 * jLabel1.setIcon(new ImageIcon(applet.getImage(applet.getCodeBase(),
		 * "short_nisthome_banner.jpg"))); jLabel2.setIcon(new
		 * ImageIcon(applet.getImage(applet.getCodeBase(), "short_logo.jpg")));
		 * } else { // Get current classloader ClassLoader cl =
		 * this.getClass().getClassLoader(); URL url =
		 * cl.getResource(ChatFrame.class
		 * .getResource("/images/short_nisthome_banner.jpg"); if (url != null)
		 * jLabel1.setIcon(new ImageIcon(url)); URL url2 =
		 * cl.getResource(ChatFrame.class.getResource("/images/short_logo.jpg");
		 * if (url2 != null) jLabel2.setIcon(new ImageIcon(url2)); }
		 * this.setSize(750, 430); jTextArea1.requestFocus(); //
		 * this.setResizable(false); this.setTitle("In conversation with " +
		 * contactAddress);
		 */
		// sipMeetingManager.createInstantMessagingSession("sip:"+contactAddress);
	}

	/****************************** GUI METHODS *******************************/

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	private void initComponents() {// GEN-BEGIN:initComponents

		this.setSize(500, 400);
		this.setTitle("00:00:00");
		this.setLayout(null);
		this.setContentPane(getContentPanel());
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);// 显示在屏幕中央
		setResizable(false);
		setVisible(true);

		// jSplitPane1 = new javax.swing.JSplitPane();
		// jPanel1 = new javax.swing.JPanel();
		// jScrollPane1 = new javax.swing.JScrollPane();
		// jTextArea2 = new javax.swing.JTextArea();
		// jPanel2 = new javax.swing.JPanel();
		// jButton1 = new javax.swing.JButton();
		// jScrollPane2 = new javax.swing.JScrollPane();
		// jTextArea1 = new javax.swing.JTextArea();
		// jLabel1 = new javax.swing.JLabel();
		// jLabel2 = new javax.swing.JLabel();
		// jPanel3 = new javax.swing.JPanel();
		// jLabel3 = new javax.swing.JLabel();
		// jButton4 = new javax.swing.JButton();
		// jButton2 = new javax.swing.JButton();
		// jButton3 = new javax.swing.JButton();
		// jBAudioAllowed = new javax.swing.JButton();
		// jBVideoAllowed = new javax.swing.JButton();

		// getContentPane().setLayout(null);

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				exitForm(evt);
			}
		});

		// jSplitPane1.setDividerLocation(200);
		// jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
		// jSplitPane1.setAlignmentX(10.0F);
		// jSplitPane1.setMaximumSize(new java.awt.Dimension(100, 100));
		// jSplitPane1.setOneTouchExpandable(false);
		// jSplitPane1.setPreferredSize(new java.awt.Dimension(100, 100));
		// jPanel1.setLayout(null);
		//
		// jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
		// public void keyPressed(java.awt.event.KeyEvent evt) {
		// jTextArea1KeyPressed(evt);
		// }
		// });
		// jTextArea2.setEditable(false);
		// jTextArea2.setLineWrap(true);
		// jTextArea1.setLineWrap(true);
		// jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		// jScrollPane1.setViewportView(jTextArea2);

		// jPanel1.add(jScrollPane1);
		// jScrollPane1.setBounds(0, 0, 460, 200);
		//
		// jSplitPane1.setTopComponent(jPanel1);
		//
		// jPanel2.setLayout(null);

		// jButton1.setText("Send");
		// jButton1.addActionListener(new java.awt.event.ActionListener() {
		// public void actionPerformed(java.awt.event.ActionEvent evt) {
		// jButton1ActionPerformed(evt);
		// }
		// });
		// jPanel2.add(jButton1);
		// jButton1.setBounds(390, 10, 63, 50);

		// jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		// jScrollPane2.setViewportView(jTextArea1);

		// jPanel2.add(jScrollPane2);
		// jScrollPane2.setBounds(0, 0, 380, 70);
		//
		// jSplitPane1.setBottomComponent(jPanel2);
		//
		// getContentPane().add(jSplitPane1);
		// jSplitPane1.setBounds(10, 70, 462, 280);
		//
		// getContentPane().add(jLabel1);
		// jLabel1.setBounds(10, 10, 460, 50);
		//
		// getContentPane().add(jLabel2);
		// jLabel2.setBounds(500, 70, 191, 310);
		//
		// jPanel3.setLayout(null);

		/*
		 * jLabel3.setText("Voice Messaging :"); jLabel3.setEnabled(false);
		 * jPanel3.add(jLabel3); jLabel3.setBounds(0, 0, 130, 20);
		 * 
		 * jButton4.setText("Right click & hold to record msg.");
		 * jButton4.setEnabled(false);
		 */
		/*
		 * jButton4.addActionListener(new java.awt.event.ActionListener() {
		 * public void actionPerformed(java.awt.event.ActionEvent evt) {
		 * jButton4ActionPerformed(evt); } });
		 */
		/*
		 * jButton4.addMouseListener(new MouseListener(){ public void
		 * mousePressed(MouseEvent m){ VoiceRecorder
		 * voiceRecorder=VoiceRecorder.getInstance(); //record voice in a buffer
		 * if(voiceRecorder.start())
		 * jButton4.setText("Release to send voice msg."); } public void
		 * mouseReleased(MouseEvent m){ VoiceRecorder
		 * voiceRecorder=VoiceRecorder.getInstance();
		 * jButton4.setText("Right click & hold to record msg."); //stop
		 * recording voice if(voiceRecorder.stop()){ //send the voice message
		 * sipMeetingManager.sendVoiceMessage( "sip:"+contactAddress,
		 * voiceRecorder.getRecord()); } } public void mouseEntered(MouseEvent
		 * m){
		 * 
		 * } public void mouseExited(MouseEvent m){
		 * 
		 * } public void mouseClicked(MouseEvent m){
		 * 
		 * } }); jPanel3.add(jButton4); jButton4.setBounds(1, 40, 230, 50);
		 */

		// getContentPane().add(jPanel3);
		// jPanel3.setBounds(500, 240, 300, 110);
		//
		// jButton2.setText("Audio");
		// jButton2.addActionListener(new java.awt.event.ActionListener()
		// {
		// public void actionPerformed(java.awt.event.ActionEvent evt)
		// {
		// jButtonAudioActionPerformed(evt);
		// }
		// });
		//
		// getContentPane().add(jButton2);
		// jButton2.setBounds(496, 10, 80, 50);
		//
		// jButton3.setText("Video");
		// jButton3.addActionListener(new java.awt.event.ActionListener()
		// {
		// public void actionPerformed(java.awt.event.ActionEvent evt)
		// {
		// jButtonVideoActionPerformed(evt);
		// }
		// });
		// getContentPane().add(jButton3);
		// jButton3.setBounds(610, 10, 80, 50);
		//
		// jBAudioAllowed.setText("禁用音频");
		// jBAudioAllowed.addActionListener(new java.awt.event.ActionListener()
		// {
		// public void actionPerformed(java.awt.event.ActionEvent evt)
		// {
		// jButtonAudioAllowedActionPerformed(evt);
		// }
		// });
		// getContentPane().add(jBAudioAllowed);
		// jBAudioAllowed.setBounds(496, 80, 80, 50);
		//
		// jBVideoAllowed.setText("禁用视频");
		// jBVideoAllowed.addActionListener(new java.awt.event.ActionListener()
		// {
		// public void actionPerformed(java.awt.event.ActionEvent evt)
		// {
		// jButtonVideoAllowedActionPerformed(evt);
		// }
		// });
		// getContentPane().add(jBVideoAllowed);
		// jBVideoAllowed.setBounds(610, 80, 80, 50);
		//
		//
		// pack();
	}// GEN-END:initComponents

	private JPanel getContentPanel() {
		contentPane = new JPanel() {
			private static final long serialVersionUID = 1L;

			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				ImageIcon img = new ImageIcon(
						ChatFrame.class
								.getResource("/images/chatDialog/chatDialog.png"));
				g.drawImage(img.getImage(), 0, 0, null);
			};
		};
		contentPane.setBackground(Color.WHITE);
		contentPane.setLayout(null);
		contentPane.add(getAccountInfo());
		contentPane.add(getJpVideoShow());
		contentPane.add(getJlCallStatus());
		contentPane.add(getJpVolumn());
		contentPane.add(getJpControls());
		return contentPane;
	}

	private JLabel getAccountInfo() {
		if (jlAccountInfo == null) {
			jlAccountInfo = new JLabel("账户信息", SwingConstants.CENTER);
			jlAccountInfo.setSize(500, 20);
			jlAccountInfo.setText("正在与：" + contactAddress + " 会话");
			jlAccountInfo.setLocation(0, LocationY);
			LocationY += jlAccountInfo.getHeight();
		}
		return jlAccountInfo;
	}

	public JPanel getJpVideoShow() {
		if (jpVideoShow == null) {
			jpVideoShow = new JPanel();
			jpVideoShow.setSize(500, 210);
			jpVideoShow.setLocation(0, LocationY);
			jpVideoShow.setLayout(null);
			// jpVideoShow.setBackground(Color.BLUE);
			jpVideoShow.setOpaque(false);
			LocationY += jpVideoShow.getHeight();
		}
		return jpVideoShow;
	}

	private JLabel getJlCallStatus() {
		if (jlCallStatus == null) {
			jlCallStatus = new JLabel("通话状态", SwingConstants.CENTER);
			jlCallStatus.setSize(500, 20);
			jlCallStatus.setText("通话状态");
			jlCallStatus.setLocation(0, LocationY);
			LocationY += jlCallStatus.getHeight();
		}
		return jlCallStatus;
	}

	private JPanel getJpVolumn() {
		jpVolumn = new JPanel();
		jpVolumn.setSize(500, 70);
		jpVolumn.setLocation(0, LocationY);
		jpVolumn.setOpaque(false);
		LocationY += jpVolumn.getHeight();
		return jpVolumn;
	}

	private JPanel getJpControls() {
		jpControls = new JPanel();
		jpControls.setSize(500, 50);
		jpControls.setLocation(0, LocationY);
		jpControls.setOpaque(false);
		jpControls.setLayout(null);

		// Core Design
		ControlButtonMouseAdapter adapter = new ControlButtonMouseAdapter();

		jbHangup = new JButton(
				new ImageIcon(
						ChatFrame.class
								.getResource("/images/chatDialog/controls/hangupButton.png")));
		jbHangup.setPressedIcon(new ImageIcon(
				ChatFrame.class
						.getResource("/images/chatDialog/controls/hangupButtonPressed.png")));
		jbHangup.setSize(29, 29);
		jbHangup.setLocation(450, 10);
		jbHangup.setOpaque(false);
		jbHangup.addMouseListener(adapter);
		jpControls.add(jbHangup);

		jbNoAudio = new JButton(
				new ImageIcon(
						ChatFrame.class
								.getResource("/images/chatDialog/controls/soundOff.png")));

		jbNoAudio.setSize(30, 30);
		jbNoAudio.setLocation(10, 10);
		jbNoAudio.setOpaque(false);
		jbNoAudio.addMouseListener(adapter);
		jpControls.add(jbNoAudio);

		jbNoVideo = new JButton(
				new ImageIcon(ChatFrame.class
						.getResource("/images/chatDialog/controls/noVideo.png")));
		jbNoVideo.setSize(30, 30);
		jbNoVideo.setLocation(45, 10);
		jbNoVideo.setOpaque(false);

		jbNoVideo.addMouseListener(adapter);
		jpControls.add(jbNoVideo);

		jbEncryptEnabled = new JButton(
				new ImageIcon(ChatDialog.class
						.getResource("/images/chatDialog/controls/encrypt.png")));
		jbEncryptEnabled.setSize(30, 30);
		jbEncryptEnabled.setLocation(80, 10);
		jbEncryptEnabled.setOpaque(false);
		jbEncryptEnabled.addMouseListener(adapter);
		jpControls.add(jbEncryptEnabled);
		
		
		
		
		jbAudioRecord = new JButton(new ImageIcon(ChatFrame.class.getResource(
		"/images/chatDialog/controls/recordButton.png")));
		jbAudioRecord.setSize(30,30);
		jbAudioRecord.setLocation(120, 10);
		jbAudioRecord.setOpaque(false);
		jbAudioRecord.setToolTipText("开始录音");
		jbAudioRecord.addMouseListener(adapter);
		jpControls.add(jbAudioRecord);
		
		jbStopAudioRecord = new JButton(new ImageIcon(ChatFrame.class.getResource(
		"/images/chatDialog/controls/recordButton.png")));
		jbStopAudioRecord.setSize(30,30);
		jbStopAudioRecord.setLocation(160, 10);
		jbStopAudioRecord.setOpaque(false);
		jbStopAudioRecord.setToolTipText("停止录音");
		jbStopAudioRecord.addMouseListener(adapter);
		jpControls.add(jbStopAudioRecord);
		
	
		
		jbCaptureScreen = new JButton(new ImageIcon(
		"iamges/chatDialog/controls/"));
		jbCaptureScreen.setSize(30, 30);
		jbCaptureScreen.setLocation(200, 10);
		jbCaptureScreen.setOpaque(false);
		jbCaptureScreen.setToolTipText("截屏");
		jbCaptureScreen.addMouseListener(adapter);
		jpControls.add(jbCaptureScreen);
		
		jbPlayer = new JButton(new ImageIcon(
		"/iamges/chatDialog/controls/recordButton.png"));
		jbPlayer.setSize(30, 30);
		jbPlayer.setLocation(240, 10);
		jbPlayer.setOpaque(false);
		jbPlayer.setToolTipText("播放視頻文件");
		jbPlayer.addMouseListener(adapter);
		jpControls.add(jbPlayer);
		
		
		jbFileTransfer = new JButton(new ImageIcon(
		"/iamges/chatDialog/controls/recordButton.png"));
		jbFileTransfer.setSize(30, 30);
		jbFileTransfer.setLocation(280, 10);
		jbFileTransfer.setOpaque(false);
		jbFileTransfer.setToolTipText("传输文件");
		jbFileTransfer.addMouseListener(adapter);
		jpControls.add(jbFileTransfer);

		return jpControls;
	}

	class ControlButtonMouseAdapter extends MouseAdapter {
		public void mouseClicked(MouseEvent event) {
			if (event.getSource().equals(jbHangup)) {
				disableAudioConversation();
				stopCall();
			} else if (event.getSource().equals(jbNoAudio)) {
				mediaManager.isAudioAllowed = !mediaManager.isAudioAllowed;
				System.out
						.println("\n\n\t\tjButtonAudioAllowedActionPerformed\n\n");
				if (mediaManager.isAudioAllowed) {
					System.out
							.println("\n\n\t\tjButtonAudioAllowedActionPerformed if\n\n");
					try {
						mediaManager.transmit.sendAudioStream.start();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else if (mediaManager.transmit.sendAudioStream != null
						&& !mediaManager.isAudioAllowed) {
					System.out
							.println("\n\n\t\tjButtonAudioAllowedActionPerformed else if\n\n");
					try {
						mediaManager.transmit.sendAudioStream.stop();
						if (mediaManager.isVideoAllowed) {
							mediaManager.transmit.sendVideoStream.start();
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else if (event.getSource().equals(jbNoVideo)) {
				mediaManager.isVideoAllowed = !mediaManager.isVideoAllowed;
				System.out
						.println("\n\n\t\tjButtonVideoAllowedActionPerformed\n\n");
				if (mediaManager.isVideoAllowed) {
					System.out
							.println("\n\n\t\tjButtonVideoAllowedActionPerformed if\n\n");
					try {
						mediaManager.transmit.sendVideoStream.start();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else if (mediaManager.transmit.sendVideoStream != null
						&& !mediaManager.isVideoAllowed) {
					System.out
							.println("\n\n\t\tjButtonVideoAllowedActionPerformed else if\n\n");
					try {
						mediaManager.transmit.sendVideoStream.stop();
						if (mediaManager.isAudioAllowed) {
							mediaManager.transmit.sendAudioStream.start();
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else if (event.getSource().equals(jbEncryptEnabled)) {
				if (Configuration.advanceSettings.getEncryptionEnable().equals(
						"0")) {
					Configuration.advanceSettings.setEncryptionEnable("1");
					jbEncryptEnabled.setToolTipText("加密传输中");
					System.err.println("加密传输中............");
				} else {
					Configuration.advanceSettings.setEncryptionEnable("0");
					jbEncryptEnabled.setToolTipText("未加密传输中");
					System.err.println("未加密传输中............");
				}
			}
			
			else if(event.getSource().equals(jbCaptureScreen))
			{
	
				@SuppressWarnings("unused")
				CaptureScreen capture = new CaptureScreen();
				
			}
			
			else if(event.getSource().equals(jbPlayer))
			{
				MediaPlayer mediaPlayer = new MediaPlayer();
				mediaPlayer.play();
			}
			
			
			else if(event.getSource().equals(jbFileTransfer))
			{
				FileTransferDialog dialog = new FileTransferDialog(ChatFrame.this);
				ReceiveFile receiver = new ReceiveFile(mediaManager.callListener.getConfiguration().contactIPAddress);
				SendFile sender = new SendFile();
				dialog.setSend(sender);
				dialog.setReceive(receiver);
			}
			
			else if(event.getSource().equals(jbStopAudioRecord))
				saveMedia.stopRecord();
			
			else if(event.getSource().equals(jbAudioRecord));
			{
				@SuppressWarnings("static-access")
				DataSource mergeDataSource = mediaManager.receiver.mergeDataSource;
				DataSource audioDataSource = mediaManager.receiver.audioDataSource;
				DataSource videoDataSource = mediaManager.receiver.videoDataSource;
				
				saveMedia = new SaveAudioVideo2File();
				
				if(null==videoDataSource&&null==audioDataSource)
					return;
				else if(videoDataSource!=null)
					saveMedia.record(videoDataSource);
				else if(audioDataSource!=null)
					saveMedia.record(audioDataSource);
				
				
//				isVideoRecordStart = !isVideoRecordStart;
//				if(isVideoRecordStart)
//				{	
//					if(videoDataSource!=null&&audioDataSource!=null)
//					{
//						try
//						{
//							mergeDataSource = Manager.createMergingDataSource(
//									new DataSource[]{audioDataSource,videoDataSource,});
//							saveMedia.record(mergeDataSource);
//						} catch (IncompatibleSourceException e)
//						{
//							e.printStackTrace();
//						}	
//						saveMedia.record(videoDataSource);
//						System.out.println("\t\t\n start");
//					
//					}
//					else if(videoDataSource!=null)
//					{
//						saveMedia.record(videoDataSource);
//						
//					}
//					else if(audioDataSource!=null)
//					{
//						saveMedia.record(audioDataSource);
//					}
//				
//				}
//				else 
//				{
//					saveMedia.stopRecord();
//				}	
			}
			

		}
	}

	protected void jButtonVideoAllowedActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		mediaManager.isVideoAllowed = !mediaManager.isVideoAllowed;
		System.out.println("\n\n\t\tjButtonVideoAllowedActionPerformed\n\n");
		if (mediaManager.isVideoAllowed) {
			System.out
					.println("\n\n\t\tjButtonVideoAllowedActionPerformed if\n\n");
			try {
				mediaManager.transmit.sendVideoStream.start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (mediaManager.transmit.sendVideoStream != null
				&& !mediaManager.isVideoAllowed) {
			System.out
					.println("\n\n\t\tjButtonVideoAllowedActionPerformed else if\n\n");
			try {
				mediaManager.transmit.sendVideoStream.stop();
				if (mediaManager.isAudioAllowed) {
					mediaManager.transmit.sendAudioStream.start();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	protected void jButtonAudioAllowedActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		mediaManager.isAudioAllowed = !mediaManager.isAudioAllowed;
		System.out.println("\n\n\t\tjButtonAudioAllowedActionPerformed\n\n");
		if (mediaManager.isAudioAllowed) {
			System.out
					.println("\n\n\t\tjButtonAudioAllowedActionPerformed if\n\n");
			try {
				mediaManager.transmit.sendAudioStream.start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (mediaManager.transmit.sendAudioStream != null
				&& !mediaManager.isAudioAllowed) {
			System.out
					.println("\n\n\t\tjButtonAudioAllowedActionPerformed else if\n\n");
			try {
				mediaManager.transmit.sendAudioStream.stop();
				if (mediaManager.isVideoAllowed) {
					mediaManager.transmit.sendVideoStream.start();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
	// sendMessage(jTextArea1.getText());
	// }

	/*
	 * private void jButtonAudioActionPerformed(java.awt.event.ActionEvent evt)
	 * {// GEN-FIRST:event_jButton2ActionPerformed // Add your handling code
	 * here: if (jButton2.getText().equalsIgnoreCase("audio")) { dial("audio");
	 * } else if (jButton2.getText().equalsIgnoreCase("stop")) {
	 * disableAudioConversation(); stopCall(); } else if
	 * (jButton2.getText().equalsIgnoreCase("cancel")) { //
	 * jTextArea2.append("You cancelled the call\n"); cancelCall(); } }//
	 * GEN-LAST:event_jButton2ActionPerformed
	 * 
	 * private void jButtonVideoActionPerformed(java.awt.event.ActionEvent evt)
	 * {// GEN-FIRST:event_jButton2ActionPerformed if
	 * (jButton3.getText().equalsIgnoreCase("Video")) { dial("video"); } else if
	 * (jButton3.getText().equalsIgnoreCase("stop")) {
	 * disableAudioConversation(); stopCall(); } else if
	 * (jButton3.getText().equalsIgnoreCase("cancel")) { //
	 * jTextArea2.append("You cancelled the call\n"); cancelCall(); }
	 * 
	 * }// GEN-LAST:event_jButton2ActionPerformed
	 * 
	 * private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//
	 * GEN-FIRST:event_jButton2ActionPerformed // Add your handling code here:
	 * VoiceRecorder voiceRecorder = VoiceRecorder.getInstance(); if
	 * (jButton4.getText().equalsIgnoreCase("talk")) { // record voice in a
	 * buffer if (voiceRecorder.start()) jButton4.setText("Stop"); } else { //
	 * stop recording voice if (voiceRecorder.stop()) {
	 * jButton4.setText("Talk"); // send the voice message
	 * sipMeetingManager.sendVoiceMessage("sip:" + contactAddress,
	 * voiceRecorder.getRecord()); } } }//
	 * GEN-LAST:event_jButton2ActionPerformed
	 */

	// private void jTextArea1KeyPressed(java.awt.event.KeyEvent evt)
	// {//GEN-FIRST:event_jTextField1KeyPressed
	// // Add your handling code here:
	// if(evt.getKeyCode()==java.awt.event.KeyEvent.VK_ENTER)
	// sendMessage(jTextArea1.getText());
	// }
	/**
	 * 
	 *
	 */
	public void cancelAudioConversation() {
		// jLabel3.setEnabled(false);
		// jButton4.setEnabled(false);
		// jButton2.setEnabled(true);
		// jButton2.setText("Audio");
		jlCallStatus.setText("通话状态");
		if (incomingMessageFrame != null) {
			incomingMessageFrame.dispose();
			incomingMessageFrame = null;
		}
		// jTextArea2.append("Your buddy has cancelled the call\n ");
	}

	/**
     * 
     *
     */
	public void disableAudioConversation() {
		// jLabel3.setEnabled(false);
		// jButton4.setEnabled(false);
		// jButton2.setText("Audio");
		jlCallStatus.setText("通话状态");
	}

	/**
	 * 
	 * @param callee
	 */
	public void enableAudioConversation(String callee) {
		AudioCall audioCall = sipMeetingManager.getCallManager().findAudioCall(
				callee);
		// if (audioCall.getVoiceMessaging())
		// {
		// jButton4.setEnabled(true);
		// jLabel3.setEnabled(true);
		// }
		// jButton2.setText("Stop");
		jlCallStatus.setText("通话中");
	}

	/**
	 * 
	 * @param callStatus
	 */
	public void updateAudioStatus(String callStatus) {
		if (callStatus.equalsIgnoreCase(Call.BUSY)) {
			// jTextArea2.append("Your buddy is currently busy, " +
			// "your call was rejected\n");
			// jButton2.setText("Audio");
			jlCallStatus.setText("通话状态");
		} else if (callStatus.equalsIgnoreCase(Call.TEMPORARY_UNAVAILABLE)) {
			// jTextArea2.append("Your buddy is currently unavailable\n");
			// jButton2.setText("Audio");
			jlCallStatus.setText("通话状态");
		} else {
			jlCallStatus.setText("呼叫中");
			// jButton2.setText("Cancel");
		}
	}

	/**
	 * 
	 *
	 */
	public void disableVideoConversation() {

	}

	/**
	 * Exit the session and the chat frame
	 */
	protected void exitForm(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_exitForm
		exitIMSession();
	}// GEN-LAST:event_exitForm

	/**
	 * Exit the session and the chat frame
	 */
	public void exitIMSession() {
		// if (jButton2.getText().equalsIgnoreCase("Stop"))
		stopCall();
		// sipMeetingManager.stopInstantMessagingSession("sip:"+contactAddress);
		chatSessionManager.removeChatSession(contactAddress);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	/***************************** APPLICATION METHODS ************************/

	/**
	 * Got a new instant message, update the gui
	 * 
	 * @param message
	 *            - message received
	 */
	public void newMessage(String message) {
		if (message.indexOf("\n") == 0)
			message = message.substring("\n".length());
		// jTextArea2.append(
		// contactAddress
		// + "> "
		// + message+"\n");
		// jTextArea2.setCaretPosition(jTextArea2.getText().length());
	}

	/**
	 * Action performed when pushing the Dial Button
	 */
	protected void dial(String callType) {
		if ("audio".equals(callType)) {
			sipMeetingManager.callManager.isAudioCall = true;
			sipMeetingManager.callManager.isVideoCall = false;
		} else if ("video".equals(callType)) {
			System.out.println("\n\n\n\t\tVideo Call\n\n\n");
			sipMeetingManager.callManager.isAudioCall = false;
			sipMeetingManager.callManager.isVideoCall = true;
		}
		sipMeetingManager.call("sip:" + contactAddress);
	}

	/**
	 * Action performed when pushing the Stop Button
	 */
	protected void stopCall() {
		sipMeetingManager.endCall("sip:" + contactAddress);
	}

	/**
	 * Action performed when pushing the Cancel Button
	 */
	protected void cancelCall() {
		sipMeetingManager.cancelCall("sip:" + contactAddress);
	}

	/**
	 * Action performed when answering NO to an incoming call
	 */
	protected void answerBusy(String caller) {
		sipMeetingManager.sendBusy(caller);
		// jButton2.setEnabled(true);
	}

	/**
	 * Action performed when answering YES to an incoming call
	 */
	protected void answerOK(String caller) {
		sipMeetingManager.answerCall(caller);
		// jButton2.setEnabled(true);
	}

	/**
	 * 
	 * @param message
	 */
	protected void sendMessage(String message) {
		while (message.indexOf("\n") == 0)
			message = message.substring("\n".length());
		if (message.length() <= 0)
			return;
		// jTextArea2.append(
		// sipMeetingManager.getMessageListener().getConfiguration().userURI
		// + "> "
		// + message+"\n");
		// jTextArea2.setCaretPosition(jTextArea2.getText().length());
		// jTextArea1.setText(null);
		sipMeetingManager.sendInstantMessage("sip:" + contactAddress, message);
	}

	/**
	 * 
	 * @param incomingMessageFrame
	 */
	public void setIncomingMessageFrame(
			IncomingMessageFrame incomingMessageFrame) {
		this.incomingMessageFrame = incomingMessageFrame;
		// jButton2.setEnabled(false);
	}

	private static final long serialVersionUID = 1L;

	private int LocationY = 0;

	private JPanel contentPane = null;
	private JLabel jlAccountInfo = null;
	private JPanel jpVideoShow = null;
	private JLabel jlCallStatus = null;
	private JPanel jpVolumn = null;
	

	private JPanel jpControls = null;
	private JButton jbHangup = null;
	private JButton jbNoAudio = null;
	private JButton jbNoVideo = null;
	private JButton jbEncryptEnabled = null;
	
	private JButton jbAudioRecord = null;
	private JButton jbStopAudioRecord = null;
	private JButton jbVideoRecord = null;
	private JButton jbStopVideoRecord = null;
	private JButton jbCaptureScreen = null;
	private JButton jbPlayer = null;
	private JButton jbFileTransfer = null;

	private boolean isAudioRecordStart = false;
	private boolean isVideoRecordStart = false;
	SaveAudioVideo2File saveMedia = null;

	// private javax.swing.ButtonGroup buttonGroup1;
	// // Variables declaration - do not modify//GEN-BEGIN:variables
	// private javax.swing.JButton jButton1;
	// private javax.swing.JButton jButton2;
	// private javax.swing.JButton jButton3;
	// private javax.swing.JButton jButton4;
	// private javax.swing.JButton jBAudioAllowed;
	// private javax.swing.JButton jBVideoAllowed;
	// private javax.swing.JLabel jLabel1;
	// private javax.swing.JLabel jLabel2;
	// private javax.swing.JLabel jLabel3;
	// private javax.swing.JPanel jPanel1;
	// private javax.swing.JPanel jPanel2;
	// private javax.swing.JPanel jPanel3;
	// private javax.swing.JScrollPane jScrollPane1;
	// private javax.swing.JScrollPane jScrollPane2;
	// private javax.swing.JSplitPane jSplitPane1;
	// private javax.swing.JTextArea jTextArea1;
	// private javax.swing.JTextArea jTextArea2;
	// End of variables declaration//GEN-END:variables

}
